package grades

import (
	"fmt"
	"sync"
)

const (
	GradeQuiz = GradeType("Quiz")
	GradeTest = GradeType("Test")
	GradeExam = GradeType("Exam")
)

var (
	students      Students
	studentsMutex sync.Mutex
)

type GradeType string

type Grade struct {
	Title string
	Type  GradeType
	Score float32
}

type Student struct {
	ID        int
	FirstName string
	LastName  string
	Grades    []Grade
}

func (s Student) Average() float32 {
	var sum float32
	for _, grade := range s.Grades {
		sum += grade.Score
	}
	if len(s.Grades) > 0 {
		return sum / float32(len(s.Grades))
	}
	return 0
}

type Students []Student

func (ss Students) GetByID(id int) (*Student, error) {
	for _, student := range ss {
		if student.ID == id {
			return &student, nil
		}
	}
	return nil, fmt.Errorf("未找到学生信息, id: %d", id)
}
